import 'package:flutter/material.dart';

class DialogDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text('对话框的例子'),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            Padding(
              padding: EdgeInsets.all(10),
              child: RaisedButton(
                onPressed: () async {
                  final select = await _showAlertDialog(context);
                  if (select) {
                    print("用户选择了确认按钮");
                  } else {
                    print("用户选择了取消按钮");
                  }
                },
                child: Text('确认对话框'),
              ),
            ),

            Padding(
              padding: EdgeInsets.all(10),
              child: RaisedButton(
                onPressed: () async {
                  final select = await _showSimpleDialog(context);
                  print("用户选择：$select");
                },
                child: Text('选择对话框'),
              ),
            ),

            Padding(
              padding: EdgeInsets.all(10),
              child: RaisedButton(
                onPressed: () async {
                  final index = await _showListDialog(context);
                  print('用户点击了第$index行');
                },
                child: Text('列表对话框'),
              ),
            ),

            Padding(
              padding: EdgeInsets.all(10),
              child: RaisedButton(
                onPressed: () async {
                  final index = await _showBottomDialog(context);
                  print('用户点击了第$index行');
                },
                child: Text('底部对话框'),
              ),
            ),

            Padding(
              padding: EdgeInsets.all(10),
              child: RaisedButton(
                onPressed: () async {
                  final date = await _showDateDialog(context);
                  print('选择的日期：$date');
                },
                child: Text('日期选择对话框'),
              ),
            ),

            Padding(
              padding: EdgeInsets.all(10),
              child: RaisedButton(
                onPressed: () async {
                  _showLoadDialog(context);
                  await Future.delayed(Duration(seconds: 3));
                  Navigator.of(context).pop();
                },
                child: Text('加载对话框'),
              ),
            ),

          ],
        ),
      ),
    );
  }

  Future _showAlertDialog(BuildContext context) {
    return showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('确认对话框'),
          content: Text('确定操作还是取消？'),
          actions: <Widget>[
            FlatButton(onPressed: (){
              Navigator.of(context).pop(true);
            }, child: Text('确定')),
            FlatButton(onPressed: (){
              Navigator.of(context).pop(false);
            }, child: Text('取消')),
          ],
        );
      },
      barrierDismissible: false,
    );
  }

  Future _showSimpleDialog(BuildContext context) {
    return showDialog(
      context: context,
      builder: (BuildContext context) {
        return SimpleDialog(
          title: Text('请选择语言'),
          children: <Widget>[
            SimpleDialogOption(
              child: Padding(
                padding: EdgeInsets.symmetric(vertical: 6),
                child: Text('中文简体'),
              ),
              onPressed: (){
                Navigator.of(context).pop('中文简体');
              },
            ),
            SimpleDialogOption(
              child: Padding(
                padding: EdgeInsets.symmetric(vertical: 6),
                child: Text('美国英语'),
              ),
              onPressed: (){
                Navigator.of(context).pop('美国英语');
              },
            ),
          ],
        );
      },
      barrierDismissible: false,
    );
  }

  Future _showListDialog(BuildContext context) {
    return showDialog(
      context: context,
      barrierDismissible: false,
      builder:(BuildContext context) {
        return Dialog(
          child: Column(
            children: <Widget>[
              ListTile(title: Text("请选择")),
              Expanded(
                child: ListView.builder(
                  itemCount: 30,
                  itemBuilder: (BuildContext context, int index){
                    return ListTile(
                      title: Text('第$index行'),
                      onTap: () {
                        Navigator.of(context).pop(index);
                      },
                    );
                  },
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  Future _showBottomDialog(BuildContext context) {
    return showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return ListView.builder(
          itemCount: 30,
          itemExtent: 50,
          itemBuilder: (BuildContext context, int index) {
            return ListTile(
              title: Text('第$index行'),
              onTap: () {
                Navigator.of(context).pop(index);
              },
            );
          },
        );
      },
    );
  }

  Future _showDateDialog(BuildContext context) {
    final today = DateTime.now();
    return showDatePicker(
      context: context,
      initialDate: today,
      firstDate: today,
      lastDate: today.add(Duration(days: 30)),
    );
  }

  Future _showLoadDialog(BuildContext context) {
    return showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return UnconstrainedBox(
          constrainedAxis: Axis.vertical,
          child: SizedBox(
            width: 280,
            child: AlertDialog(
              content: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  CircularProgressIndicator(),
                  Padding(
                    padding: EdgeInsets.only(top: 26),
                    child: Text('正在加载，请稍后...'),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}